package com.zhongkai.web.control.ticket;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhongkai.json.JSONArray;
import com.zhongkai.json.JSONException;
import com.zhongkai.json.JSONObject;
import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.config.User;
import com.zhongkai.model.declare.TSbCcssbxx;
import com.zhongkai.service.ticket.Atm_reprintService;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.web.control.book.ParamType;

@Controller
public class Atm_reprintControl extends ParamType{
	@Autowired
	private Atm_reprintService atmreprintservice;
	
	@RequestMapping(value="/system/ticket/ATM_reprint.do",method = RequestMethod.GET)
	public String atm_print(HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		JSONArray jsontax,jsonticket;
		JSONObject totalticket=new JSONObject();
		PrintWriter out=response.getWriter();
		User user =(User)request.getSession().getAttribute("user");
		String no=request.getParameter("ccdjh");		//车船登记号
		if(no==null || !no.matches("^[0-9]*[1-9][0-9]*$")){		//判断车船登记号是否非法
			out.write(CheckWord.showMsg("非法参数！", "/system/search/unisearch.jsp?module=ticket/ATM_reprint.do",request));
			return null;
		}
		Integer ccdjh=Integer.valueOf(no);		//生成整型车船登记号
		TDjCcdjxx ccdjxx=(TDjCcdjxx) atmreprintservice.selectById(TDjCcdjxx.class, ccdjh);		//根据车船登记号查找该车船登记信息
		if(ccdjxx==null){
			out.write(CheckWord.showMsg("找不到该车船登记号的信息！", "/system/search/unisearch.jsp?module=ticket/ATM_reprint.do",request));
			return null;
		}
		
		List taxList= atmreprintservice.searchcomb(ccdjh);
		if(taxList==null || taxList.size()==0 ){
			out.write(CheckWord.showMsg("没有需要重打税票的记录！", "/system/search/unisearch.jsp?module=ticket/ATM_reprint.do",request));
			return null;
		}
		//生成车船登记信息的JSON数据
		//ls.add(ccdjxx);
		//js.append("carinfo", ls);
		//js.append("carinfo", ls);
		//System.out.println(js.toString());
		//传出数据
		request.setAttribute("checked", true);
		request.setAttribute("carinfo", ccdjxx);
		request.setAttribute("taxinfo", CombinateTList(taxList));//界面显示申报信息
		//组合成JSON数据
		//js.put("taxticket",taxList);;
		//System.out.println(js.toString());
		jsontax=atmreprintservice.CombinateTax(taxList);
		//System.out.println(jsondata.length());
		//取得税票号码并输出
		List tpjclist=atmreprintservice.select("from TPzJc where pzzlDm='02' and yprDm=? order by zb,sphmQ asc",user.getUserIdentifier());
		
		
		if (tpjclist==null || tpjclist.size()==0) //如果无数据，税票状态返回。
		{
			out.write(CheckWord.showMsg("税票已用完！", "/system/search/unisearch.jsp?module=ticket/ATM_reprint.do",request));
			return null;
		}
		else
		{
			jsonticket=atmreprintservice.CombinateTicket(tpjclist,jsontax.length());
			request.setAttribute("ticketinfo", jsonticket.toString());//显示税票
			totalticket.put("totalprint", jsontax.length());
			totalticket.put("realticketnum",jsonticket.length());
			totalticket.put("ticketnum",jsonticket.length());
			request.setAttribute("ticketnum",totalticket.toString());//显示有多少税票
		}
		request.setAttribute("printinfo",jsontax.toString()); //显示打印信息
		
		return "ATM_reprint_form.jsp";
	}
	
	private List CombinateTList(List sourcelist) throws JSONException
	{
		List sourcetax=new ArrayList();
		List delaytax=new ArrayList();
		Map tmptax;
		Date tmps,tmpe;
		for(Iterator source=sourcelist.iterator();source.hasNext();)
		{
			Object[] nsxx=(Object[]) source.next();;
			TSbCcssbxx bxx=(TSbCcssbxx)nsxx[1];
			if(bxx.getZnjBj()!=null)
			{
				delaytax.add(bxx);
			}
			else
			{
				sourcetax.add(atmreprintservice.pojotomap(bxx));
			}
		}
			
		for(Iterator source=sourcetax.iterator();source.hasNext();)
		{
			tmptax=(Map) source.next();
			tmptax.put("znj", 0);
			for(Iterator sourcede=delaytax.iterator();sourcede.hasNext();)
			{
				TSbCcssbxx bxx=(TSbCcssbxx)sourcede.next();
				
				tmps=(Date)tmptax.get("sbqxRq");
				tmpe=bxx.getSbqxRq();
				if(tmps.compareTo(tmpe)==0)
				{
					tmptax.put("znj", bxx.getYbtJe());
					tmptax.put("ybtJe", bxx.getYbtJe()+bxx.getJsyj()); //根据滞纳金数据得来
					sourcede.remove();
					break;
				}
				
			}
		}
		return sourcetax;
	}

	 

	
}
